<?php
$do = 'open_'.$_REQUEST['do'];
error_reporting(-1);
if(function_exists($do)){
	$do();
}else{
	die('who?');
}


function getConfig(){
	return array( //配置信息
		'url_base'		=> 	'http://wap.sz-map.com/',  //地图网网址
		'url_search'	=>	'http://wap.sz-map.com/search?paging=y', //查询
		);
}

//搜索
function open_search(){
	$type = $_REQUEST['type'];
	$key  = urlencode($_REQUEST['key']);
	switch ($type){
		case 'route':
			$param = '&st=1&kw='.$key;
			break;
		case 'site':
			$param = '&st=0&kw='.$key;
			break;
		default:
			$param = '&st=0&kw='.$key;
			break;
	}
	$C = getConfig();
	$data = search($C['url_search'].$param);
	echo json_encode($data);
}

//获取站点
function open_site(){
	$real = htmlspecialchars_decode(base64_decode(($_REQUEST['real'])));
	echo json_encode(getSiteInfo($real));
}

//获取线路
function open_route(){
	$real = htmlspecialchars_decode(base64_decode(($_REQUEST['real'])));
	echo json_encode(getRouteInfo($real));
}


//查询
function search($url){
	$C = getConfig();
	$result = array('total'=>0,'list'=>array());
	$html = getHtml($url);
	preg_match('/&nbsp;<span class="cr">(.*?)<\/span>&nbsp;/i', $html,$preg_total);
	$total = isset($preg_total[1]) ? $preg_total[1]:0;
	if(!$total){
		return $result;
	}
	preg_match_all('/<a href=\'(.*?)\'><span style=\"color:blue;\">(.*?)<\/span>(.*?)\((.*?)\)(.*?)<\/a>/i', $html,$preg_route);
	$result['total'] = $total;
	foreach($preg_route[0] as $k=>$v){
		$result['list'][] = array(	
				'href'	=>	base64_encode($preg_route[1][$k]),
				'name'	=>	$preg_route[2][$k],
				'desc'	=>	strtr($preg_route[4][$k],array('<span>'=>'','</span>'=>'')));
	}
	return $result;
}

//获取站点信息
function getSiteInfo($real){
	$C = getConfig();
	$url = $C['url_base'].$real.'<br/>';
	$html = getHtml($url);
	$html = str_replace("\n",'', $html);
	$preg_real = '/<a href=\"(.*?)\">(.*?)<span style=\"font-weight: bold;\">(.*?)<\/span>(.*?)<span>\(下趟车距离<strong style=\"color: red;font-size:14px;\">(.*?)<\/strong>站\)<\/span>/i';
	preg_match_all($preg_real, $html,$preg_route);
	$route_list = array();
	foreach($preg_route[0] as $k=>$V){
		$route_list[] = array(
				'href'	=> base64_encode($preg_route[1][$k]),
				'name'	=> $preg_route[3][$k],
				'distance' => strtr($preg_route[5][$k],array('<span>'=>'','</span>'=>'')),
				);
	}
	return array('total'=>count($route_list),'list'=>$route_list);
}

//获取线路信息
function getRouteInfo($real){
	$C = getConfig();
	$url = $C['url_base'].$real;
	$html = getHtml($url);
	$html = str_replace("\n",'', $html);
	$preg_real = '/<a class=\"(.*?)\" href=\"(.*?)\">(.*?)<\/a>/i';
	preg_match_all($preg_real, $html,$preg_site);
	$preg_real = '/进站时间：(.*?)<\/span>/i';
	preg_match_all($preg_real, $html,$preg_time);
	$routeInfo = array();
	foreach($preg_site[0] as $k=>$v){
		$data = array(
				'href'	=>base64_encode($preg_site[2][$k]),
				'name'	=>$preg_site[3][$k],
				'time'	=>0,//有时间的是有车停靠的站点
				);
		if($preg_site[1][$k] == 'hba'){
			$data['time'] = array_shift($preg_time[1]);
		}
		
		$routeInfo[] = $data;
	}
	return array('total'=>count($routeInfo),'list'=>$routeInfo);
}

//获取查询html
function getHtml($url){
	$result = file_get_contents($url);
	if($result){
		return $result;
	}
	return "";
}

?>